#include <iostream>
#include<sstream>
#include <string>
#include<vector>
#include "Category.h"
using namespace std;

Category:: Category(string name)
{
	_name=name;
	_usedBudget=0;
}

string Category::toString()
{ostringstream outstr;
	for(int i=0;i<expenseItem.size();i++)
		outstr<<expenseItem[i].toString();
	return outstr.str();
}

string Category :: toString(const vector<int>& result)
{
	ostringstream outstr;
	for(int i=0;i<result.size();i++)
		outstr << i+1 << ". " << expenseItem[result[i]].toString();
	return outstr.str();
}

string Category :: getName()
{
	return _name;
}

void Category :: updateName(string newName)
{
	_name=newName;
	return;
}

void Category :: addItem(DateTime date, string name, double amount, PaymentStatus status)
{
	expenseItem.push_back(Item(date,name,amount,status));
	return;
}

void Category :: updateItem(int index, DateTime date, string name, double amount, PaymentStatus status)
{
	expenseItem[index].updateData(date,name,amount,status);
	return;
}
	
void Category :: deleteItem(int index)
{
	vector<Item> :: iterator myIter=expenseItem.begin();
	for(int i=0;i<index;i++)
		myIter++;
	expenseItem.erase(myIter);
		return;
}

vector<int> Category :: search(DateTime date)
{
	vector<int> resultIndex;
	for(int i=0;i<expenseItem.size();i++)
		if(expenseItem[i].compareDate(date))
			resultIndex.push_back(i);
	return resultIndex;
}

vector<Item> Category:: getExpenseItem()
{
	return expenseItem;
}

void Category:: insertItem(Item item, int index)
{
	vector<Item>:: iterator myIter=expenseItem.begin();
	for(int i=0;i<index;i++)
		myIter++;
	expenseItem.insert(myIter,item);
}

double Category :: getUsedBudget(const DateTime& date)
{
	double amount=0;
	for(int i=0;i<expenseItem.size();i++)
		if(expenseItem[i].compareDate(date))
			amount+=expenseItem[i].getAmount();
	return amount;
}


void Category :: calculatePayable(double amount, vector<int>& payableIndex)
{
	for(int i=0;i<expenseItem.size();i++)
		if(expenseItem[i].getStatus()==PAYABLE)
		{
			amount+=expenseItem[i].getAmount();
			payableIndex.push_back(i);
		}
	return;
}